package dao

import (
	"bms/db"
	"bms/model"
)

func AddSession(session *model.Session) error {
	sql := "insert into sessions values (?,?,?)"
	_, err := db.Db.Exec(sql, session.SessionId, session.Username, session.UserId)
	if err != nil {
		return err
	}
	return nil
}

func DeleteSession(sessionId string) error {
	sql := "delete from sessions where session_id=?"
	_, err := db.Db.Exec(sql, sessionId)
	if err != nil {
		return err
	}
	return nil
}

func GetSessionBySessionId(sessionId string) *model.Session {
	sql := "select session_id,username,user_id from sessions where session_id=?"
	row := db.Db.QueryRow(sql, sessionId)
	session := &model.Session{}
	row.Scan(&session.SessionId, &session.Username, &session.UserId)
	return session
}
